<template>
    <div class="content">
        <TableLayout form-title=""> 
             <el-form :model="formInline" class="formInline" label-width="90px" label-position="right" slot="form" v-collapse="true">          
                 <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="所属省">
                            <province v-model="formInline.fProvinceId"></province>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="所属市">
                            <city v-model="formInline.fCityId" :province="formInline.fProvinceId"></city>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="所属区县">
                            <region v-model="formInline.fAreaId" :city="formInline.fCityId"></region>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="41"> 
                    <el-col :span="8">
                        <!--<el-form-item label="学校名称">
                            <el-input v-model="formInline.schoolName" placeholder="请输入内容"></el-input>
                        </el-form-item>-->

                        <el-form-item label="学校名称">
                            <school v-model="formInline.schoolName" :provinceId="formInline.fProvinceId" :cityId="formInline.fCityId" :areaId="formInline.fAreaId" style="width:100%" @change='schoolChange'></school>
                        </el-form-item>

                    </el-col> 
                     <el-col :span="8">
                        <el-form-item label="银行账户名">
                            <el-input v-model="formInline.bankName" placeholder="请输入内容" :maxlength="12"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="银行卡号" prop="bankCard">
                            <el-input v-model="formInline.bankCard" placeholder="请输入内容" :maxlength="19"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="申请日期">
                            <el-row>  
                                <el-col :span="11">
                                    <el-date-picker :editable="false" v-model="formInline.startDate" type="date" format="yyyy-MM-dd" placeholder="选择开始日期" :picker-options="formInline.pickerOptions1" @change="dateChange1" style="width:100%">
                                    </el-date-picker>
                                </el-col>
                                <el-col :span="2" align="center">至</el-col>
                                <el-col :span="11">
                                    <el-date-picker :editable="false" v-model="formInline.endDate" type="date" placeholder="选择结束日期" :picker-options="formInline.pickerOptions2"  @change="dateChange2" style="width:100%">
                                    </el-date-picker>
                                </el-col> 
                            </el-row>             
                        </el-form-item>
                    </el-col>
                    
                    <el-col :span="8">
                        <el-form-item label="状态">
                            <el-select v-model="formInline.typeStatus" style="width:100%" >
                                <el-option v-for="item in statusType" :key="item.typeStatustId" :label="item.statusName" :value="item.typeStatustId"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="top-line">
                    <el-col :span="4" :offset="20"> 
                        <el-button type="primary" class="top-btn" @click="onSubmit">查询</el-button>
                    </el-col>
                </el-row>
            </el-form>

            <div class="withdrawals" slot = "showPanel">
                <el-row :gutter="41">
                    <el-col :span="12">昨日结算（元）：<span style="margin-left:5px;">{{yesterday.yesterdayMoney}}</span></el-col>
                    <el-col :span="12">累计结算金额（元）：<span style="margin-left:5px;">{{yesterday.allMoney}}</span></el-col>
                </el-row>
            </div>

            <div class="table-title" slot="table-title">
                <div class="btnPosition">
                    <el-button v-if="batchState" type="text" class="btn-success" @click="selectFinish()">完成</el-button>
                    <el-button v-if="batchState" type="text" class="btn-success" @click="selectCancel()">取消</el-button>
                    <el-button :disabled="batchState == '1'" @click="goToAddRole">批量完成</el-button>
                    <el-button  @click="exportBtn">导出</el-button>
                </div>
            </div>
            <el-table border ref="multipleTable" :data="dataApply"  style="width: 100%" @selection-change="handleSelectionChange">

                    <!--<el-table-column v-if="batchState" type="selection" width="55" :selectable="selectable"></el-table-column>-->
                   
                    <el-table-column type="selection" width="55" :selectable="selectable" align="center"></el-table-column>

                    <el-table-column align="center" prop="sCreateTime" label="申请日期">
                        <template scope="scope">
                            <span>{{scope.row.sCreateTime || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sSchoolName"  label="学校名称">
                        <template scope="scope">
                            <span>{{scope.row.sSchoolName || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sMoney"  label="结算金额（元）">
                        <template scope="scope">
                            <span>{{scope.row.sMoney || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sUserRealName"  label="银行账户名">
                        <template scope="scope">
                            <span>{{scope.row.sUserRealName || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sCardNo"  label="银行卡号">
                        <template scope="scope">
                            <span>{{scope.row.sCardNo || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="bankName"  label="所属银行">
                        <template scope="scope">
                            <span>{{scope.row.bankName || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sBankDesc"  label="开户行">
                        <template scope="scope">
                            <span>{{scope.row.sBankDesc || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center"  label="结算状态">
                        <template scope="scope">
                            <span v-if="scope.row.sStatus=='0'">已结算</span>
                            <span v-if="scope.row.sStatus=='1'">处理中</span>
                            <span v-if="scope.row.sStatus=='2'">已驳回</span>
                            <span v-if="scope.row.sStatus=='3'">未审核</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="sUpdateTime"  label="完成时间">
                        <template scope="scope">
                            <span>{{scope.row.sUpdateTime || '--'}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="updator"  label="操作人">
                        <template scope="scope">
                            {{scope.row.updator == null? '--': scope.row.updator }}
                        </template>
                    </el-table-column>

                    <el-table-column align="left" label="操作" width="220">
                        <template scope="scope">
                            <el-button type="text" class="btn-success" @click="adoptBtn(scope.row, '1')" v-if="scope.row.sStatus=='3'">通过</el-button>
                            <el-button type="text" class="btn-success" @click="refuse(scope.row, '2')" v-if="scope.row.sStatus=='3'">拒审</el-button>
                            <el-button type="text" class="btn-success" @click="successBtn(scope.row, '0')" v-if="scope.row.sStatus=='1'">完成</el-button>                    
                            <el-button type="text" class="btn-success" @click="failBtn(scope.row, '2')" v-if="scope.row.sStatus=='1'">失败</el-button>
                            <!--<el-button type="text" class="btn-success" v-if="scope.row.sStatus=='0'||scope.row.sStatus=='2'">--</el-button>-->
                            <el-button type="text" class="btn-success" @click="checkDetails(scope.row)" >查看详情</el-button>
                        </template>
                    </el-table-column>    
            </el-table>
            <div class="pagination-container" style="text-align:right">
                    <el-pagination
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="listQuery.total"
                        :current-page.sync="listQuery.page"
                        :page-sizes="config.pageSizes"
                        :page-size="config.pageSize"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
            </div>
        </TableLayout>

        <!-- 批量完成 -->
        <batch-complete :batchCompleteData='batchCompleteData' @search='searchFetchData'></batch-complete>

        <!-- 失败 -->
        <fail-withdraw :failData='failData' @search='searchFetchData'></fail-withdraw>

        <!-- 驳回 -->
        <reject-withdraw :rejectData='rejectData' @search='searchFetchData'></reject-withdraw>

    </div>
</template>

<script>
import {countSettlementMoney, pageSettlement, exportSettlement, updateSettlement} from 'api/settleManagement';
// import {test} from 'api/login';
import TableLayout from 'components/TableLayouts';
import { mapState, mapActions } from 'vuex';
import { mapGetters } from 'vuex';
import moment from 'moment';
import province from 'components/CascadeRegion/province';
import city from 'components/CascadeRegion/city';
import region from 'components/CascadeRegion/region';
import school from 'components/SchoolClasss/school';
import batchThrough from './batchThrough';
import batchComplete from './batchComplete';
import failWithdraw from './failWithdraw';
import rejectWithdraw from './rejectWithdraw';
import rejectCheck from './rejectCheck';

export default{
    name: 'cardSettle',
    components: {TableLayout, rejectCheck, province, city, region, batchThrough, batchComplete, failWithdraw, rejectWithdraw, school },
    data() {
        return {
            pId: '',
            batchState: '',
            batchCompleteData: {
                dialogState: false,
                multipleSelection: []
            },
            failData: {
                dialogState: false,
                settlementIds: [],
                status: ''
            },
            rejectData: {
                dialogState: false,
                settlementIds: [],
                status: ''
            },
            yesterday: {
                allMoney: '',
                yesterdayMoney: ''
            },
            formInline: {// form表单数据
                schoolName: '',
                schoolId: '',
                fProvinceId: '',
                fAreaId: '',
                fCityId: '',
                typeStatus: '',
                bankCard: '',
                bankName: '',
                startDate: '', // 注册开始时间
                endDate: '', // 注册结束时间
                sRemark: '',
                pickerOptions1: {},
                pickerOptions2: {}
            },
            dialog: {
                dialogFormVisible1: false,
                dialogFormVisible2: false
            },
            statusType: [
                {
                    typeStatustId: '',
                    statusName: '请选择'
                },
                {
                    typeStatustId: '0',
                    statusName: '已结算'
                }, 
                {
                    typeStatustId: '1',
                    statusName: '处理中'
                },
                {
                    typeStatustId: '2',
                    statusName: '已驳回'
                },
                {
                    typeStatustId: '3',
                    statusName: '未审核'
                }],
            dataApply: [],
            loading: true,
            listQuery: {// 分页
                page: 1,
                total: 0
            }
        }
    },
    computed: {
        ...mapState({
            config: state => state.config.table
        }),
        ...mapGetters([
            'uid',
            'token'
        ])
    },
    created() {
        this.searchFetchData();
        this.searchData();
    },
    methods: {
        ...mapActions(['ChangePageSize']),
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        // 
        searchData() { 
            countSettlementMoney(
                this.formInline.fProvinceId, 
                this.formInline.fCityId, 
                this.formInline.fAreaId,
                // this.formInline.schoolName,
                this.formInline.schoolId, 
            ).then(response => {
                this.yesterday = response.data.content;
                this.yesterday.allMoney = parseFloat(this.yesterday.allMoney).toFixed(2);
                this.yesterday.yesterdayMoney = parseFloat(this.yesterday.yesterdayMoney).toFixed(2);
                console.log('this.yesterday', this.yesterday);
            
            });
        },

        schoolChange(val) {
            this.formInline.schoolId = val;
        },

        // 查询
        onSubmit() {// 表单查询
            this.listQuery.page = 1;
            this.loading = true;
            this.searchData();
            this.searchFetchData();
        },

        // 分页
        handleSizeChange(val) {
            this.listQuery.page = 1;
            this.loading = true;
            this.ChangePageSize(val);
            this.searchFetchData();
        },

        handleCurrentChange(val) {
            this.loading = true;
            this.listQuery.page = val;
            this.searchFetchData();
        },

         // 开始日期選擇
        dateChange1(date) {
            this.formInline.pickerOptions2 = {
                disabledDate(time) {
                    return time.getTime() < new Date(date).getTime() - 864e5;
                }
            }
        },
        // 结束日期选择
        dateChange2(date) {
            this.formInline.pickerOptions1 = {
                disabledDate(time) {
                    return time.getTime() > new Date(date).getTime();
                }
            }
        },


        searchFetchData() {
            //  sUserRealName, sCardNo
            let startDate = this.formInline.startDate==''?'':moment(this.formInline.startDate).format('YYYY-MM-DD');
            let endDate = this.formInline.endDate==''?'':moment(this.formInline.endDate).format('YYYY-MM-DD');
            pageSettlement(
                this.listQuery.page, 
                this.config.pageSize, 
                this.formInline.fProvinceId, 
                this.formInline.fCityId, 
                this.formInline.fAreaId, 
                this.formInline.schoolId,
                startDate,
                endDate,
                this.formInline.typeStatus,
                this.formInline.bankName,
                this.formInline.bankCard).then((response) => {

                    if (response.data.status==200) {
                    this.dataApply = response.data.content.list;
                    this.listQuery.total = response.data.content.pagination.total;
                    this.loading = false;   
                } else {
                    this.$message({
                        type: 'warning',
                        message: response.data.errorMsg
                    })
                }
                });
        },

        // 选择完成
        selectFinish() {
            if (this.batchState == '1') {
                if (this.multipleSelection.length == 0) {
                    this.$message.info('请选择列表项');     
                } else {
                    this.batchCompleteData.dialogState = true;
                    this.batchCompleteData.multipleSelection = this.multipleSelection;
                    console.log('this.batchCompleteData.multipleSelection', this.batchCompleteData.multipleSelection)
                }
            }
        },

        // 取消选择
        selectCancel() {
            this.$refs.multipleTable.clearSelection();
            this.multipleSelection = [];
            this.batchCompleteData.multipleSelection = [];
            // this.batchThroughData.multipleSelection = [];
            this.batchState = '';
        },

        selectable(row) {
            return row.sStatus === '1';
        },

        // 批量完成
        goToAddRole() {
            this.batchState = '1';
            this.$refs.multipleTable.clearSelection();
            this.multipleSelection = [];
            // this.batchThroughData.multipleSelection = [];
        },

        // 导出
        exportBtn() {
            // let startDate = this.formInline.startDate==''?'':moment(this.formInline.startDate).format('YYYY-MM-DD');
            // let endDate = this.formInline.endDate==''?'':moment(this.formInline.endDate).format('YYYY-MM-DD');
            window.location.href = `${LOCAL_API.fyq}/settlement/exportSettlement?sBeginTime=${this.formInline.startDate}&sEndTime=${this.formInline.endDate}&sStatus=${this.formInline.typeStatus}&sSchoolId=${this.formInline.schoolId}&token=${this.token}`; 
        },

        // 通过
        adoptBtn(row, status) {

            let settlementIds = [];
            settlementIds.push(row.pId);
            console.log('settlementIds', row);
            updateSettlement(settlementIds, status).then(response => {
                this.searchFetchData();
                this.$message({
                    type: 'success',
                    message: response.data.errorMsg
                })
            });  
        },
        
        // 完成
        successBtn(row, status) {
            let settlementIds = [];
            settlementIds.push(row.pId);
            this.$confirm('确认完成打款？', ' ', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                updateSettlement(settlementIds, '0', this.formInline.sRemark).then(response => {
                    this.searchFetchData();
                    this.$message({
                        type: 'success',
                        message: response.data.errorMsg
                    });
                });  
            }).catch(() => {
                this.$message.info('已取消');
            });
        },

        // 失败
        failBtn(row, status) {
            let settlementIds = [];
            settlementIds.push(row.pId);
            // this.dialog.dialogFormVisible2=true;
            this.failData.settlementIds = settlementIds;
            console.log('111', this.failData.settlementIds);
            this.failData.dialogState = true;
            this.failData.status = status;
        },

        // 拒审
        refuse(row, status) {
            let settlementIds = [];
            settlementIds.push(row.pId);
            // this.dialog.pid = row.schoolId;
            // this.dialog.dialogFormVisible1=true;
            this.rejectData.settlementIds = settlementIds;
            console.log('111', this.rejectData.settlementIds);
            this.rejectData.dialogState = true;
            this.rejectData.status = status;
        },

        // 查看详情
        checkDetails(row) {
            this.$router.push({
                path: `cardDetail/${row.pId}`
            });
            // this.$router.push('checkDetails', params: pId);
        }
      
    }
}
</script>
<style lang="scss" scoped>
.content {
    position: relative;
    padding: 0px;
    $bgcolor:#fff;
    .withdrawals{
        width:100%;
        background:#fff;
        padding:30px 100px;
        margin-bottom:20px;
        box-shadow: 0px 1px 2px #999;
        -webkit-box-shadow: 0px 1px 2px #999;
        .el-col{
            text-align:left;
            span{
                margin-left:10px;
                font-size:18px;
                display:inline-block;
            }
        }
    }
    .formInline {
        .formBtn{
            width: 100%;
            height: 70px;
            border-top: 1px solid #dddddd;
            position: relative;
            .btnPosition{
                position: absolute;
                right:0px; 
                top: 18px;
            }
        }
    }
}
</style>  
